package com.keste.adf.model.applicationmodule;


import com.keste.adf.model.applicationmodule.common.OnlineQuizAM;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.domain.DBSequence;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Sat Jan 04 17:18:49 IST 2014
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class OnlineQuizAMImpl extends ApplicationModuleImpl implements OnlineQuizAM {
    
    private String currentUser=null;
    /**
     * This is the default constructor (do not remove).
     */
    public OnlineQuizAMImpl() {
    }


    /**
     * Container's getter for UsersVO.
     * @return UsersVO
     */
    public ViewObjectImpl getUsersVO() {
        return (ViewObjectImpl)findViewObject("UsersVO");
    }

    /**
     * Container's getter for RefSubjects1.
     * @return RefSubjects1
     */
    public ViewObjectImpl getRefSubjectsVO() {
        return (ViewObjectImpl)findViewObject("RefSubjectsVO");
    }
    
    public void initializeQuestionADay(Integer subjectId)
    {
        ViewObjectImpl questionADayVO = getQuestionADayVO();
        questionADayVO.setNamedWhereClauseParam("Bind_SubjectID", subjectId);
     //   questionADayVO.setApplyViewCriteriaName("findByPublishedDate");
        Date dt = new Date();
        SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yy");
        String newStartDateVal = dateformatJava.format(dt);
    
        questionADayVO.setNamedWhereClauseParam("Bind_PublishedDate", newStartDateVal);
//        System.out.println(questionADayVO.getQuery());
        questionADayVO.executeQuery();
        questionADayVO.getRowCount();
        questionADayVO.setCurrentRowAtRangeIndex(0);
 //       System.out.println("Options Found: "+this.getQADAnswersVO().getRowCount());
        
   //     questionADayVO.getRowAtRangeIndex(0).setAttribute("PublishedDate", arg1);
        
        ViewObjectImpl answersVO = this.getQADAnswersVO();
        answersVO.executeQuery();
        answersVO.getRowCount();
    }


    /**
     * Container's getter for Questions1.
     * @return Questions1
     */
    public ViewObjectImpl getAllQuestionsVO() {
        return (ViewObjectImpl)findViewObject("AllQuestionsVO");
    }


    /**
     * Container's getter for QuestionADay1.
     * @return QuestionADay1
     */
    public ViewObjectImpl getQuestionADayVO() {
        return (ViewObjectImpl)findViewObject("QuestionADayVO");
    }

    /**
     * Container's getter for Answers1.
     * @return Answers1
     */
    public ViewObjectImpl getQADAnswersVO() {
        return (ViewObjectImpl)findViewObject("QADAnswersVO");
    }

    /**
     * Container's getter for QuestionADayVOToAnswersVOVL1.
     * @return QuestionADayVOToAnswersVOVL1
     */
    public ViewLinkImpl getQuestionADayVOToAnswersVOVL1() {
        return (ViewLinkImpl)findViewLink("QuestionADayVOToAnswersVOVL1");
    }

    /**
     * Container's getter for UserResponses1.
     * @return UserResponses1
     */
    public ViewObjectImpl getUserResponsesVO() {
        return (ViewObjectImpl)findViewObject("UserResponsesVO");
    }

    /**
     * Container's getter for UsersVOToUserResponsesVOVL1.
     * @return UsersVOToUserResponsesVOVL1
     */
    public ViewLinkImpl getUsersVOToUserResponsesVOVL1() {
        return (ViewLinkImpl)findViewLink("UsersVOToUserResponsesVOVL1");
    }
    
    public void setCurrentUser(String loginId)
    {
        ViewObjectImpl usersVO = getUsersVO();
        usersVO.setApplyViewCriteriaName("findByLoginID");
        usersVO.setNamedWhereClauseParam("Bind_LoginId", loginId);
        usersVO.executeQuery();
        long usrId=0;
        
        if(usersVO.getRowCount() == 1)
        {
            usrId = ((DBSequence)usersVO.getRowAtRangeIndex(0).getAttribute("UserId")).getValue();
 //           userId = (Number)usersVO.getRowAtRangeIndex(0).getAttribute("UserId");
        }
        Number userid = new Number(usrId);
        usersVO.removeApplyViewCriteriaName("findByLoginID");
        usersVO.executeQuery();
        
        Key ky = new Key(new Object[]{userid});
        
        Row rw = usersVO.getRow(ky);
        if(rw != null)
        {
            currentUser = loginId;
            usersVO.setCurrentRow(rw);
        }
        
    }
    
    public void resetCurrentUser()
    {
        currentUser = null;
        getUsersVO().reset();
    }
    
    public void submitAnswers(List qadAnswers)
    {
        ViewObjectImpl usersVO = getUsersVO();
        ViewObjectImpl userResponsesVO = getUserResponsesVO();
        
        ViewObjectImpl questionADayVO = getQuestionADayVO();
        
        Row currentQuestion = questionADayVO.getCurrentRow();
        Number questionId = (Number)currentQuestion.getAttribute("QuestionId");
        Number examId = (Number)currentQuestion.getAttribute("ExamId");
        
        Date todayDate = new Date();
        
        for(int iIndex = 0;iIndex < qadAnswers.size();iIndex++)
        {
            Row newAnswer = userResponsesVO.createRow();
            System.out.println(newAnswer.getAttribute("UserId"));
            newAnswer.setAttribute("ExamId", examId);
            newAnswer.setAttribute("QuestionId", questionId);
            newAnswer.setAttribute("DateOfAnswer", todayDate);
            newAnswer.setAttribute("UserAnswerId", qadAnswers.get(iIndex));
            userResponsesVO.insertRow(newAnswer);
        }
        
        getDBTransaction().commit();
        
        userResponsesVO.getRowCount();
        return;
    }
    
    public Boolean canUserSubmitThisQuestion(Number examId,Number questionId)
    {
        ViewObjectImpl usersVO = this.getUsersVO();
        if(currentUser != null && usersVO.getCurrentRow() != null)
        {//findByExamAndQuestionId
            ViewObjectImpl userResponsesVO = getUserResponsesVO();
            userResponsesVO.setApplyViewCriteriaName("findByExamAndQuestionId");
            userResponsesVO.setNamedWhereClauseParam("Bind_ExamId", examId);
            userResponsesVO.setNamedWhereClauseParam("Bind_QuestionId", questionId);
            
            if(userResponsesVO.getRowCount()==0)
            {
                return true;
            }
        }
        return false;
    }
}
